package org.zebra.mybatis.plus.ext;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.zebra.mybatis.plus.ext.datapermission.DataPermissionInterceptorRegister;
import org.zebra.mybatis.plus.ext.redundancy.RedundancyFieldInterceptorRegister;

import java.util.List;

/**
 * 动态拦截器注册
 *
 * @author zhanghongbin
 */
public class DynamicMybatisPlusInterceptor {

    @Async
    @EventListener
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        List<TableInfo> tableInfos = TableInfoHelper.getTableInfos();
        if (tableInfos.isEmpty()) {
            return;
        }
        // 注册冗余字段变化
        RedundancyFieldInterceptorRegister.register(tableInfos);
        // 注册数据权限
        DataPermissionInterceptorRegister.register(tableInfos, contextRefreshedEvent);
    }
}
